package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.AuditMallUserWithdrawalsRecordReq;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.MallUserWithdrawalsRecordAdminReq;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.dto.MallProfitTemplateAdminPageDTO;
import com.sc.nft.entity.vo.MallProfitTemplateAdminPageVO;
import com.sc.nft.entity.vo.mall.MallUserWithdrawalsRecordAdminPage;
import com.sc.nft.enums.MallWithdrawType;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallUserWithdrawalsRecordService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.admin.controller
 * @Project：sc_metago
 * @name：MallUserWithdrawalsRecordController
 * @Date：2024/5/21 14:42
 * @Filename：MallUserWithdrawalsRecordController
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/userWithdrawalsRecord")
@Api(tags = "小程序提现模块")
public class MallUserWithdrawalsRecordController {

    private final MallUserWithdrawalsRecordService mallUserWithdrawalsRecordService;
    private final Redisson redisson;

    @ApiOperation(value = "小程序提现记录", notes = "小程序提现记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallUserWithdrawalsRecordAdminPage> page(@RequestBody MallUserWithdrawalsRecordAdminReq mallUserWithdrawalsRecordAdminReq) {
        return PageResult.success(mallUserWithdrawalsRecordService.getAdminPage(mallUserWithdrawalsRecordAdminReq.getStartTime(), mallUserWithdrawalsRecordAdminReq.getEndTime(), mallUserWithdrawalsRecordAdminReq.getUserTel(), mallUserWithdrawalsRecordAdminReq.getWithdrawalsStatus(),
                mallUserWithdrawalsRecordAdminReq.getPageNo(), mallUserWithdrawalsRecordAdminReq.getPageSize()));
    }


    @ApiOperation(value = "审核通过", notes = "审核通过", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pass", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result pass(@Valid @RequestBody AuditMallUserWithdrawalsRecordReq req) {
        // 商城提现锁
        RLock lock = redisson.getLock(RedisKeyEnum.MALL_WITHDRAWAL_LOCK.getKey() + req.getId());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                mallUserWithdrawalsRecordService.pass(req.getId(), req.getRemark(), SysMemberPool.getMember());
            } else {
                return Result.fail("当前人数过多，请稍后再试!");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "审核拒绝", notes = "审核拒绝", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/turnDown", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result turnDown(@Valid @RequestBody AuditMallUserWithdrawalsRecordReq req) {
        // 商城提现锁
        RLock lock = redisson.getLock(RedisKeyEnum.MALL_WITHDRAWAL_LOCK.getKey() + req.getId());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                mallUserWithdrawalsRecordService.turnDown(req.getId(), req.getRemark(), SysMemberPool.getMember());
            } else {
                return Result.fail("当前人数过多，请稍后再试!");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

}
